#include "process.h"
#include <string.h>

#define NUM 101
#define STYLE '='

// version 2
void FlushProcess(double total, double current)
{

    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    
    const char* lable = "|/-\\";
    int len = strlen(lable);

    // 不需要自己循环（填充=）
    int num = (int)(current * 100 / total);
    for(int i = 0; i < num; i ++)
    {
        buffer[i] = STYLE;
    }

    double rate = current / total;

    static int cnt = 0;
    cnt %= len;

    printf("[%-100s][%.1f%%][%c]\r", buffer, rate * 100, lable[cnt]);
    fflush(stdout);
    
    cnt ++;
}

// 无法使用
void process_v1()
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    
    const char* lable = "|/-\\";
    int len = strlen(lable);

    int cnt = 0;
    while(cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", buffer, cnt, lable[cnt % len]);
        fflush(stdout);
        buffer[cnt] = STYLE;
        cnt ++;
        sleep(1);
    }
    printf("\n");
} 
